using System.Collections;
using Game.Core;
using Game.Main.Controller;
using Game.Manager;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Game.Main.View
{
	public class SplashView : BaseView
	{
		private const int STEP_NUM = 9;

		private SceneController clr;

		private Transform icon;

		private float iconY;

		private float startX;

		private float endX;

		private float curX;

		private float step;

		private float moveX;

		private float moveSpeed;

		private int curStepIndex;

		private int curPointIndex;

		private Transform[] points;

		private GameObject closeMask;

		public SplashView(SceneController clr)
			: base("prefabs/ui/scene/splashview", Singleton<UIManager>.Instance.uiLayer)
		{
			viewName = "SplashView";
			this.clr = clr;
		}

		protected override void OnLoaded()
		{
			icon = GetChild("Icon");
			iconY = icon.position.y;
			startX = GetChild("LeftPoint").position.x;
			endX = GetChild("RightPoint").position.x;
			step = (endX - startX) / 9f;
			Transform child = GetChild("Points");
			points = new Transform[child.childCount];
			for (int i = 0; i < points.Length; i++)
			{
				points[i] = child.GetChild(i);
			}
			curX = startX - 5f;
			endX += 5f;
			moveSpeed = (endX - curX) / 5.5f;
			icon.position = new Vector3(curX, iconY, 0f);
			closeMask = GetChild("CloseMask").gameObject;
			UIEventSystem.Get(closeMask).AddEventListener(EventTriggerType.PointerDown, OnCloseMaskDown);
			Client.Instance.StartCoroutine(MainLoop());
		}

		public IEnumerator MainLoop()
		{
			while (transform != null && curX < endX)
			{
				curX += Time.deltaTime * moveSpeed;
				icon.position = new Vector3(curX, iconY, 0f);
				if (curStepIndex < 9 && curX >= startX + (float)(curStepIndex + 1) * step)
				{
					if (curStepIndex > 1 && curStepIndex < 6)
					{
						points[curPointIndex].position = new Vector3(startX + (float)(curStepIndex + 1) * step, icon.position.y, icon.position.z);
						points[curPointIndex].GetComponent<Animator>().Play("Anim", 0);
						Singleton<AudioManager>.Instance.PlaySound("sound_bug_point_" + (curPointIndex + 1));
						curPointIndex++;
					}
					curStepIndex++;
				}
				if (curX >= endX)
				{
					clr.OnSplashViewClose();
				}
				yield return new WaitForEndOfFrame();
			}
		}

		private void OnCloseMaskDown(BaseEventData evt)
		{
			clr.OnSplashViewClose();
		}
	}
}
